package com.jr.controller;

import com.jr.entry.BlogText;
import com.jr.pojo.PageHelper;
import com.jr.service.BlogTextService;
import com.jr.util.ResultObject;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController /*替换@RestController=@Controller + @ResponseBody*/
@RequestMapping("/Blog/")
public class BlogTextController {
    @Autowired
    private BlogTextService blogTextMapperService;
    @Autowired
    private ResultObject result;

    @RequestMapping("showAll")
    public ArrayList<BlogText> showAll(PageHelper pageHelper) {
        RowBounds rowBounds = new RowBounds((pageHelper.getPage() - 1) * 5, 5);
        ArrayList<BlogText> list = blogTextMapperService.showAllBlogText(rowBounds);
        return list;
    }

    @RequestMapping("showAllBlogs")
    public ResultObject showAllBlogs(){
        result.setStatus(400);
        result.setResult(blogTextMapperService.showAllBlogTexts());
        return result;
    }


    @RequestMapping("showBlogTextByClickNumber")
    public ResultObject showBlogTextByClickNumber(){
        result.setStatus(400);
        ArrayList<BlogText> blogTexts = new ArrayList<>();
        int i=blogTextMapperService.showBlogTextByClickNumber().size();
        for (int j=0;j<i;j++){
            BlogText blogText = blogTextMapperService.showBlogTextByClickNumber().get(j);
            blogTexts.add(blogText);
        }
        result.setResult(blogTexts);
        return result;
    }

    @RequestMapping("showBlogTextDetail")
    public ResultObject showBlogTextDetail(BlogText blogText){
        result.setStatus(400);
        result.setResult(blogTextMapperService.showBlogTextDetail(blogText));
        return result;
    }
    @RequestMapping("showBlogTextByType")
    public ArrayList<BlogText> showBlogTextByType(BlogText blogText){
        return blogTextMapperService.showBlogTextByType(blogText);
    }

    @RequestMapping("showSuggest")
    public ResultObject showSuggest(BlogText blogText){
        result.setStatus(400);
        result.setResult(blogTextMapperService.showSuggest(blogText));
        return result;
    }

    @RequestMapping("changeClickNumber")
    public ResultObject changeClickNumber(Integer blogid){
        result.setStatus(400);
        result.setResult(blogTextMapperService.changeClickNumber(blogid));
        return result;
    }
    @RequestMapping("insert")
    public ResultObject insert(BlogText blogText){
        result.setStatus(400);
        result.setResult(blogTextMapperService.addBlogText(blogText));
        return result;
    }
    @RequestMapping("delete")
    public ResultObject delete(BlogText blogText){
        result.setStatus(400);
        result.setResult(blogTextMapperService.removeBlogText(blogText));
        System.out.println(result.getResult());
        return result;
    }
    @RequestMapping("update")
    public ResultObject update(BlogText blogText){
        result.setStatus(400);
        result.setResult(blogTextMapperService.editBlogText(blogText));
        System.out.println(result.getResult()+"////////////");
        return result;
    }

    /*获得数据库总数据*/
    @RequestMapping("count")
    public int countBlogText(BlogText blogText) {
        int i = blogTextMapperService.countBlogText(blogText);
        return i;
    }
}
